package com.buptlogistics.creditinvestigation.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.buptlogistics.creditinvestigation.R;
import com.buptlogistics.creditinvestigation.app.BaseFragment;
import com.buptlogistics.creditinvestigation.app.BaseFragmentActivity;
import com.buptlogistics.creditinvestigation.fragment.LoginFragment;
import com.buptlogistics.creditinvestigation.fragment.RegistFragment;

import it.neokree.materialtabs.MaterialTab;
import it.neokree.materialtabs.MaterialTabHost;
import it.neokree.materialtabs.MaterialTabListener;


public class LoginRegistActivity extends BaseFragmentActivity implements MaterialTabListener {
    private BaseFragment login = new LoginFragment();
    private BaseFragment registe = new RegistFragment();

    MaterialTabHost tabHost;
    ViewPager pager;
    ViewPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_regist);

        // init view pager
        adapter = new ViewPagerAdapter(getSupportFragmentManager());

        initViews();
        initEvents();


    }

    /**
     * （非 Javadoc）
     * <p>
     * Title: onResume
     * </p>
     * <p>
     * Description:resume
     * </p>
     *
     * @see android.support.v4.app.FragmentActivity#onResume()
     */
    @Override
    protected void onResume() {
        super.onResume();
        // ///////////////////////////////
        //////////有没有更好的方法，判断 用户的信息已经储存完成之后 关闭该界面
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //if (AVUser.getCurrentUser() != null) {
                //    Log.i("LeanCloudUtil", "has logined,finish context");
                //    LoginRegistActivity.this.finish();
                //} else {
                //    Log.i("LoginRegistActivity", "not login");
                //}
            }
        }, 500);
        // ///////////////////////////////////
        Log.i("LoginRegistActivity", "LoginRegistActivity onResume");

    }

    /**
     * （非 Javadoc）
     * <p>
     * Title: onDestroy
     * </p>
     * <p>
     * Description:
     * </p>
     */
    @Override
    protected void onDestroy() {
        Log.i("onDestroy", "LoginRegistActivity onDestroy");
        super.onDestroy();
    }

    @Override
    protected void initViews() {
        tabHost = (MaterialTabHost) this.findViewById(R.id.login_regist_materialTabHost);
        pager = (ViewPager) this.findViewById(R.id.login_regist_pager);
    }

    @Override
    protected void initEvents() {

        pager.setAdapter(adapter);
        pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // when user do a swipe the selected tab change
                tabHost.setSelectedNavigationItem(position);

            }
        });

        // insert all tabs from pagerAdapter data
        for (int i = 0; i < adapter.getCount(); i++) {
            tabHost.addTab(
                    tabHost.newTab()
                            .setText(adapter.getPageTitle(i))
                            .setTabListener(this)
            );

        }
    }

    @Override
    public void onTabSelected(MaterialTab materialTab) {
        pager.setCurrentItem(materialTab.getPosition());
    }

    @Override
    public void onTabReselected(MaterialTab materialTab) {

    }

    @Override
    public void onTabUnselected(MaterialTab materialTab) {

    }


    @Override
    protected void onPause() {
        super.onPause();
    }


    private class ViewPagerAdapter extends FragmentStatePagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);

        }

        public Fragment getItem(int num) {
            Fragment returnFragment = null;
            switch (num) {
                case 1:
                    returnFragment = registe;
                    break;
                case 0:
                    returnFragment = login;
                    break;
            }
            return returnFragment;
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            String title = "";
            switch (position) {
                case 1:
                    title = getResources().getString(R.string.regist_txt);
                    break;
                case 0:
                    title = getResources().getString(R.string.login_txt);
                    break;
            }
            return title;
        }

    }

}
